package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.pickaddress.ChangeAddressDialog;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 发布工具设备
 */
public class PostToolsActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_post_tools_company)
    EditText etPostToolsCompany;
    @Bind(R.id.tv_post_tools_city)
    TextView tvPostToolsCity;
    @Bind(R.id.ll_post_tools_city)
    LinearLayout llPostToolsCity;
    @Bind(R.id.et_post_tools_product)
    EditText etPostToolsProduct;
    @Bind(R.id.et_post_tools_brand)
    EditText etPostToolsBrand;
    @Bind(R.id.et_post_tools_website)
    EditText etPostToolsWebsite;
    @Bind(R.id.et_post_tools_backup)
    EditText etPostToolsBackup;
    @Bind(R.id.et_post_tools_contacts)
    EditText etPostToolsContacts;
    @Bind(R.id.et_post_tools_tel)
    EditText etPostToolsTel;
    @Bind(R.id.ll_post_tools_choose_photo)
    LinearLayout llPostToolsChoosePhoto;
    @Bind(R.id.tv_post_tools_commit)
    TextView tvPostToolsCommit;
    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;
    private String province="";
    private String city="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_tools);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }


    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_post_tools, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    private void initEvent() {
        tvPostToolsCommit.setOnClickListener(this);
        llPostToolsChoosePhoto.setOnClickListener(this);
        llPostToolsCity.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ll_post_tools_city:
                ChangeAddressDialog mChangeAddressDialog = new ChangeAddressDialog(
                        this);
                mChangeAddressDialog.setAddress("浙江", "杭州");
                mChangeAddressDialog.show();
                mChangeAddressDialog.setAddresskListener(new ChangeAddressDialog.OnAddressCListener()
                                                         {
                                                             @Override
                                                             public void onClick(String province, String city) {
                                                                 PostToolsActivity.this.province=province;
                                                                 PostToolsActivity.this.city=city;
                                                                 tvPostToolsCity.setText(province + " " + city);
                                                             }
                                                         }
                );
                break;
            case R.id.ll_post_tools_choose_photo:
                Intent intent=new Intent(this,ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.tv_post_tools_commit:

                if (files == null || files.length == 0) {
                    T.showShort(this, "请选择照片");
                } else {
                    String company = etPostToolsCompany.getText().toString();
                    String city=tvPostToolsCity.getText().toString();
                    String product = etPostToolsProduct.getText().toString();
                    String brand = etPostToolsBrand.getText().toString();
                    String website = etPostToolsWebsite.getText().toString();
                    String backup = etPostToolsBackup.getText().toString();
                    String contacts = etPostToolsContacts.getText().toString();
                    String tel = etPostToolsTel.getText().toString();
                    if (Utils.isEmpty(company)) {
                        T.showShort(this, "请输入供应商名称");
                    } else if (Utils.isEmpty(city)) {
                        T.showShort(this, "请选择地区");
                    } else if (Utils.isEmpty(product)) {
                        T.showShort(this, "请输入产品名称");
                    } else if (Utils.isEmpty(brand)) {
                        T.showShort(this, "请输入主要成分");
                    } else if (Utils.isEmpty(website)) {
                        T.showShort(this, "请输入产品工艺");
                    } else if (Utils.isEmpty(backup)) {
                        T.showShort(this, "请输入规格");
                    }else if (Utils.isEmpty(contacts)) {
                        T.showShort(this, "请输入联系人姓名");
                    } else if (Utils.isEmpty(tel)) {
                        T.showShort(this, "请输入联系人电话");
                    } else {
                        Loading.show(this, getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }

    }

    private void commit() {
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_TOOLS, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(PostToolsActivity.this,e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(PostToolsActivity.this, response);
                }
            },files,fileKeys,getParams(etPostToolsCompany.getText().toString(),province,city,etPostToolsProduct.getText().toString(),
                    etPostToolsBrand.getText().toString(),etPostToolsWebsite.getText().toString(),etPostToolsBackup.getText().toString(),
                    etPostToolsContacts.getText().toString(),etPostToolsTel.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public OkHttpClientManager.Param[] getParams(String title, String province, String city, String product, String brand, String website,
                                                 String backup, String  contacts,
                                                 String tel){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(this,"id")),
                new OkHttpClientManager.Param("title", title),
                new OkHttpClientManager.Param("province", province),
                new OkHttpClientManager.Param("city", city),
                new OkHttpClientManager.Param("product", product),
                new OkHttpClientManager.Param("brand", brand),
                new OkHttpClientManager.Param("website", website),
                new OkHttpClientManager.Param("backup", backup),
                new OkHttpClientManager.Param("contacts", contacts),
                new OkHttpClientManager.Param("tel", tel)};
    }
}
